/* eslint-disable indent */
/* eslint-disable no-negated-condition */
/* eslint-disable react/no-direct-mutation-state */
import React from 'react';
import PropTypes from 'prop-types';
import routes from './routes';
import cloneDeep from 'lodash/cloneDeep';
import {Popconfirm, Table, Button, Form, Row, Col, Card, Spin, message as msg, Modal} from 'antd';
import {getDetail, getDealerSaleInformations, getCarriers} from './api';
import {FORM_OPTIONS, TABLE, FIXED_COLUMN_WIDTH} from '../constants';
import Return from '../common/Return';
import FormItem from 'antd/lib/form/FormItem';
import {hasPermissions} from './utils';
import {formatAmount} from '../utils';
import {PERMISSION} from './constants';
import {newOrUpdateOrderValidator, saleOrderValidator} from './validator';
import {injectIntl, language} from './intl';
import WrappedSelect from '../common/WrappedSelect';
import DealerSelectPanel from '../common/DealerSelect/SelectEdit';
import TextInput from 'Shared/components/TextInput';
import InputAmount from '../common/InputAmount';
import SearchInput from 'Shared/components/SearchInput';
import BankSelectPanel from './BankSelectPanel';
import SaleOrderPanel from './SaleOrderPanel';
import SaleOrderDetailPanel from './SaleOrderDetailPanel';
import ProductCategoryPanel from './ProductCategoryPanel';
import LocalizeInputNumber from '../common/LocalizeInputNumber';
import {currency, saleOrderPriceTerms, paymentType, saleOrderTransportType, proformaInvoiceSource, isYesNo} from '../Enum';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import WrappedDatePicker from '../common/WrappedDatePicker';
import ProductCategory from './ProductCategory';
const {Description} = DescriptionList;
// eslint-disable-next-line prefer-const
export class NewPanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        validate: false,
        valiSaleOrder: false,

        data: {
            countryOfOrigin: 'People\'s Republic of China',
            dataSource: 3,
            brandId: '',
            commodity: 'Chery brand passenger car',
            ifUseBatchNumberToPrint: 0,
            querySource: false,
        },
        details: [],
        carrierList: [],
        showBankSelectPanel: false,
        showSaleOrderPanel: false,
        showProductCategoryPanel: false,
        showSaleOrderDetailPanel: false,
        showProductCategory: false,
        isUnitVolume: false
    };

    componentDidMount() {
        this.getDetailInfo();
    }

    componentWillUnmount() {
        this.isunmount = true;
    }
    getDetailInfo = () => {
        if(this.props.id) {
            this.setState(({
                loading: true
            }));
            getDetail(this.props.id)
                .then(res => {
                    if(res.ok) {
                        if(this.state.data.brandId) {
                            const condition = {
                                brandId: this.state.data.brandId
                            };
                            this.getCarrier(condition);
                        }
                        if(!this.state.data.brandId && this.props.initEnum.brands.length !== 0) {
                            const condition = {
                                brandId: this.props.initEnum.brands[0].value
                            };
                            this.getCarrier(condition);
                        }
                        res.data.ifUseBatchNumberToPrint = res.data.ifUseBatchNumberToPrint ? 1 : 0;
                        this.setState({
                            loading: false,
                            data: res.data,
                            details: res.data.saleProformaInvoiceDetails
                        });
                    } else
                        this.setState(({
                            loading: false
                        }));
                });
        }
    }
    getCarrier = condition => {
        getCarriers(condition).then(res => {
            if(res.ok)
                this.setState({
                    carrierList: res.data.content.map(item => ({
                        text: `${'('}${item.code}${')'}${item.name}`,
                        value: item.id
                    }))
                });
        });
    }
    handleFilterChange = (value, name) => {
        if(this.state.data.brandId && name === 'dealerId') {
            const condition = {
                brandId: this.state.data.brandId
            };
            this.getCarrier(condition);
        }
        if(!this.state.data.brandId && name === 'dealerId') {
            const condition = {
                brandId: this.props.initEnum.brands[0].value
            };
            this.getCarrier(condition);
        }
        if(name === 'dataSource') {
            this.state.validate = false;
            this.state.valiSaleOrder = false;
            this.state.details = [];
            this.state.data = {
                countryOfOrigin: 'People\'s Republic of China',
                dataSource: 3
            };
        }
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    // eslint-disable-next-line react/sort-comp
    onClickCurrency = (value, name) => {
        if(this.state.data.dataSource === proformaInvoiceSource.整车销售订单 && this.state.details.length > 0 && Boolean(this.state.data.currency) && this.state.data.currency !== value)
            Modal.confirm({
                title: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch2',
                    defaultMessage: '提示'
                }),
                content: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch.currency',
                    defaultMessage: '变更币种，将会清空清单数据，是否清空？'
                }),
                onOk: () => this.onChangeSwitchOther(value, name)
            });
        else {
            const data = Object.assign({}, this.state.data, {
                [name]: value,
                bankId: '',
                bankName: '',
                bankAccount: ''
            });
            this.setState({
                data,
                hasOperat: true
            });
        }
    }
    onClickTradeMethodId = (value, name) => {
        if(this.state.data.dataSource === proformaInvoiceSource.整车销售订单 && this.state.details.length > 0 && Boolean(this.state.data.tradeMethodId) && this.state.data.tradeMethodId !== value)
            Modal.confirm({
                title: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch2',
                    defaultMessage: '提示'
                }),
                content: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch.tradeMethodId',
                    defaultMessage: '变更贸易方式，将会清空清单数据，是否清空？'
                }),
                onOk: () => this.onChangeSwitchOther(value, name)
            });
        else {
            const data = Object.assign({}, this.state.data, {
                [name]: value,
                bankId: '',
                bankName: '',
                bankAccount: ''
            });
            this.setState({
                data,
                hasOperat: true
            });
        }
    }
    onClickPaymentType = (value, name) => {
        if(this.state.data.dataSource === proformaInvoiceSource.整车销售订单 && this.state.details.length > 0 && Boolean(this.state.data.paymentType) && this.state.data.paymentType !== value)
            Modal.confirm({
                title: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch2',
                    defaultMessage: '提示'
                }),
                content: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch.paymentType',
                    defaultMessage: '变更付款方式，将会清空清单数据，是否清空？'
                }),
                onOk: () => this.onChangeSwitchOther(value, name)
            });
        else {
            const data = Object.assign({}, this.state.data, {
                [name]: value
            });
            this.setState({
                data,
                hasOperat: true
            });
        }
    }
    
    onChangeSwitchOther = (value, name) => {
        if(name === 'currency' || name === 'tradeMethodId') {
            const data = Object.assign({}, this.state.data, {
                [name]: value,
                bankId: '',
                bankName: '',
                bankAccount: '',
            });
            this.setState({
                data,
                details: [],
                hasOperat: true
            });
        } else {
            const data = Object.assign({}, this.state.data, {
                [name]: value,
            });
            this.setState({
                data,
                details: [],
                hasOperat: true
            });
        }
    }
    onClickDeliveryTerms = (value, name) => {
        if(this.state.data.dataSource === proformaInvoiceSource.整车销售订单 && this.state.details.length > 0 && Boolean(this.state.data.deliveryTerm) && this.state.data.deliveryTerm !== value)
            Modal.confirm({
                title: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch2',
                    defaultMessage: '提示'
                }),
                content: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch.deliveryTerm',
                    defaultMessage: '变更运输方式，将会清空清单数据，是否清空？'
                }),
                onOk: () => this.onChangeSwitchOther(value, name)
            });
        else {
            const details = this.state.details;
            if(value === saleOrderTransportType.Others || value === saleOrderTransportType['By Air']) {
                let totalShipmentAmount = 0;
                if(details) {
                    details.forEach(v => {
                        totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber;
                    });
                    this.setState({
                        data: Object.assign({}, this.state.data, {
                            shipmentAmount: totalShipmentAmount,
                            [name]: value
                        }),
                        details: details.map(v => Object.assign({}, v, {
                            containerNumber: 0,
                            unitShipmentAmount: 0
                        }))
                    });
                } else {
                    const data = Object.assign({}, this.state.data, {
                        [name]: value
                    });
                    this.setState({
                        data,
                        hasOperat: true
                    });
                }
            }
            if(value === saleOrderTransportType.Container ||
                value === saleOrderTransportType.Railway ||
                value === saleOrderTransportType['By Sea']) {
                    let totalShipmentAmount = 0;
                    if(details) {
                        details.forEach(v => {
                            totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber;
                        });
                        this.setState({
                            data: Object.assign({}, this.state.data, {
                                shipmentAmount: totalShipmentAmount,
                                [name]: value
                            })
                        });
                    } else {
                        const data = Object.assign({}, this.state.data, {
                            [name]: value
                        });
                        this.setState({
                            data,
                            hasOperat: true
                        });
                    }
            }
            if(value === saleOrderTransportType['RO-RO']) {
                let totalShipmentAmount = 0;
                if(details) {
                    details.forEach(v => {
                        totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                    });
                    this.setState({
                        data: Object.assign({}, this.state.data, {
                            shipmentAmount: totalShipmentAmount,
                            [name]: value
                        }),
                        details: details.map(v => Object.assign({}, v, {
                            containerNumber: 0
                        }))
                    });
                } else {
                    const data = Object.assign({}, this.state.data, {
                        [name]: value
                    });
                    this.setState({
                        data,
                        hasOperat: true
                    });
                }
            }
        }
    }
    
    onClickSaleOrderPriceTerms = (value, name) => {
        if(this.state.data.dataSource === proformaInvoiceSource.整车销售订单 && this.state.details.length > 0 && Boolean(this.state.data.salesOrderPriceTerms) && this.state.data.salesOrderPriceTerms !== value)
            Modal.confirm({
                title: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch2',
                    defaultMessage: '提示'
                }),
                content: this.props.intl.formatMessage({
                    id: 'newPanel.message.switch.salesOrderPriceTerms',
                    defaultMessage: '变更价格条款，将会清空清单数据，是否清空？'
                }),
                onOk: () => this.onChangeSwitchOther(value, name)
            });
        else {
            const details = this.state.details;
            if(value === saleOrderPriceTerms.FOB ||
                value === saleOrderPriceTerms.FCA ||
                value === saleOrderPriceTerms.EXW)
                this.setState({
                    details
                });
            if(value === saleOrderPriceTerms.CPT ||
                value === saleOrderPriceTerms.CFR) {
                const data = this.state.data;
                if(data.deliveryTerms === saleOrderTransportType.Container ||
                    data.deliveryTerms === saleOrderTransportType.Railway ||
                    data.deliveryTerms === saleOrderTransportType['By Sea']) {
                    let totalShipmentAmount = 0;
                    if(details)
                        details.forEach(v => {
                            totalShipmentAmount += v.unitShipmentAmount * v.containerNumber;
                        });
                    this.setState({
                        data: Object.assign({}, data, {
                            shipmentAmount: totalShipmentAmount,
                            [name]: value
                        }),
                        hasOperat: true
                    });
                } else if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
                    let totalShipmentAmount = 0;
                    if(details) {
                        details.forEach(v => {
                            totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                        });
                        this.setState({
                            data: Object.assign({}, this.state.data, {
                                shipmentAmount: totalShipmentAmount,
                                [name]: value
                            }),
                            details: details.map(v => Object.assign({}, v, {
                                containerNumber: 0
                            }))
                        });
                    }
                } else
                    this.setState({
                        data: Object.assign({}, this.state.data, {
                            [name]: value
                        }),
                        hasOperat: true
                    });
            } else if(value === saleOrderPriceTerms.CIF ||
                value === saleOrderPriceTerms.CIP) {
                const data = this.state.data;
                if(data.deliveryTerms === saleOrderTransportType.Container ||
                    data.deliveryTerms === saleOrderTransportType.Railway ||
                    data.deliveryTerms === saleOrderTransportType['By Sea']) {
                    let totalShipmentAmount = 0;
                    let totalInsuranceAmount = 0;
                    if(details)
                        details.forEach(v => {
                            totalShipmentAmount += v.unitShipmentAmount * v.containerNumber;
                            totalInsuranceAmount += (v.unitInsuranceAmount ? v.unitInsuranceAmount : 0) * v.quantity;
                        });
                    this.setState({
                        data: Object.assign({}, data, {
                            shipmentAmount: totalShipmentAmount,
                            insuranceAmount: totalInsuranceAmount,
                            [name]: value
                        }),
                        hasOperat: true
                    });
                } else if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
                    let totalShipmentAmount = 0;
                    let totalInsuranceAmount = 0;
                    if(details)
                        details.forEach(v => {
                            totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                            totalInsuranceAmount += (v.unitInsuranceAmount ? v.unitInsuranceAmount : 0) * v.quantity;
                        });
                    this.setState({
                        data: Object.assign({}, data, {
                            shipmentAmount: totalShipmentAmount,
                            insuranceAmount: totalInsuranceAmount,
                            [name]: value
                        }),
                        hasOperat: true
                    });
                } else
                    this.setState({
                        data: Object.assign({}, this.state.data, {
                            [name]: value
                        }),
                        hasOperat: true
                    });
            } else {
                const data = Object.assign({}, this.state.data, {
                    [name]: value,
                    payableAccount: value !== saleOrderPriceTerms.FCA ? '' : this.state.data.payableAccount,
                    carrierId: (value === saleOrderPriceTerms.FOB ||
                        value === saleOrderPriceTerms.FCA ||
                        value === saleOrderPriceTerms.EXW) ? this.state.data.carrierId : ''
                });
                this.setState({
                    data,
                    hasOperat: true
                });
            }
        }
    }
    onClickReturnBtn = () => {
        this.props.onCancel();
    }
    
    onSubmit = () => {
        const isValid = newOrUpdateOrderValidator(this.state.data, this.state.details);
        if(!isValid) {
            this.setState({
                validate: true
            });
            return;
        }
        this.setState({
            loading: true,
        });
        this.props.onSubmit(this.state.data, this.state.details).then(() => {
            if(this.success)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    
    onSelectDealer = (condition = {}) => {
        if(this.props.initEnum.brands.length === 1)
            this.state.data.brandId = this.props.initEnum.brands[0].value;
        if(!this.state.data.brandId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.choseBrand',
                defaultMessage: '请先选择品牌'
            }));
        if(this.state.data.brandId)
            this.setState(state => {
                const newData = cloneDeep(state.data);
                return {
                    hasOperat: true,
                    data: {
                        ...newData,
                        dealerId: condition.id,
                        dealerCode: condition.code,
                        dealerName: condition.name,
                        address: condition.address,
                        zipCode: condition.zipCode,
                        contactNumber: condition.contactPhone,
                        country: condition.countryName,
                        marketDepartmentName: condition.marketDepartmentName
                    }
                };
            });
            if(condition.id && this.state.data.dataSource === 3) {
                this.queryDealerSaleInformation(condition.id);
                this.setState({
                    details: [],
                });
            }
            if(!condition.id) {
                const newData = cloneDeep(this.state.data);
                this.setState({
                    data: {
                        ...newData,
                        currency: undefined,
                        tradeMethodId: '',
                        tradeMethodName: '',
                        salesOrderPriceTerms: undefined,
                        paymentType: undefined,
                        account: '',
                        carrierId: '',
                        carrierCode: '',
                        carrierName: '',
                        shippingPortId: '',
                        shippingPortCode: '',
                        shippingPortName: '',
                        destinationPort: '',
                        CNPJ: '',
                        buyer: '',
                        contact: '',
                        TELEX: '',
                        deliveryTerms: undefined,
                        address: '',
                        zipCode: '',
                        contactNumber: '',
                        country: '',
                        marketDepartmentName: '',
                    },
                    details: []
                });
            }
    };
    queryDealerSaleInformation = dealerId => {
        const id = dealerId;
        const queryCondition = {
            brandId: this.state.data.brandId,
            dealerId: id,
            isCBU: true
        };
        getDealerSaleInformations(queryCondition).then(res => {
            if(res.ok)
                if(this.state.data.brandId) {
                    const condition = {
                        brandId: this.state.data.brandId
                    };
                    this.getCarrier(condition);
                }
                if(!this.state.data.brandId) {
                    const condition = {
                        brandId: this.props.initEnum.brands[0].value
                    };
                    this.getCarrier(condition);
                }
            this.setState(state => {
                const newData = cloneDeep(state.data);
                return {
                    hasOperat: true,
                    data: {
                        ...newData,
                        currency: res.data.currency ? res.data.currency : null,
                        tradeMethodId: res.data.tradeMethodId ? res.data.tradeMethodId : null,
                        tradeMethodName: res.data.tradeMethodName ? res.data.tradeMethodName : null,
                        salesOrderPriceTerms: res.data.priceClause,
                        paymentType: res.data.paymentType,
                        account: res.data.account,
                        carrierId: res.data.carrierId,
                        shippingPortId: res.data.shippingPortId,
                        shippingPortCode: res.data.shippingPortCode,
                        shippingPortName: res.data.shippingPortName,
                        destinationPort: res.data.destinationPort,
                        CNPJ: res.data.CNPJ,
                        buyer: res.data.buyer,
                        contact: res.data.contact,
                        TELEX: res.data.TELEX,
                        deliveryTerms: res.data.deliveryTerms
                    },
                };
            });
        });
    }
    onClickBankSearchBtn = () => {
        if(!this.state.data.brandId && this.props.initEnum.brands.length !== 1)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.choseBrand',
                defaultMessage: '请先选择品牌'
            }));
        if(!this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.choseDealer',
                defaultMessage: '请先选择总代'
            }));
        if(!this.state.data.tradeMethodId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.tradeMethodId',
                defaultMessage: '请先选择贸易方式'
            }));
        if(!this.state.data.currency)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.currency',
                defaultMessage: '请先选择币种'
            }));
        this.setState({showBankSelectPanel: true});
    }
    onCloseBankSelectPanel = () => {
        this.setState({showBankSelectPanel: false});
    }
    onSelectAddedBank = records => {
        this.setState({
            data: Object.assign({}, this.state.data, {
                bankId: records.id,
                bankAccount: records.account,
                bankName: records.name
            }),
            hasOperat: true,
            showBankSelectPanel: false
        });
    }
    onClickAddSaleOrderBtn = () => {
        // const isValid = saleOrderValidator(this.state.data);
        // if(!isValid) {
        //     this.setState({
        //         valiSaleOrder: true,
        //     });
        //     return;
        // }
        this.setState({showSaleOrderPanel: true});
    }
    onCloseSaleOrderPanel = () => {
        this.setState({showSaleOrderPanel: false});
    }
    onSelectSaleOrder = records => {
        this.setState({
            data: Object.assign({}, this.state.data, {
                saleOrderId: records.id,
                saleOrderCode: records.code,
                // currency: records.currency,
                // tradeMethodId: records.tradeMethodId,
                // tradeMethodName: records.tradeMethodName,
                // paymentType: records.paymentType,
                // deliveryTerms: records.transportType,
                // salesOrderPriceTerms: records.salesOrderPriceTerms,
            }),
            hasOperat: true,
            showSaleOrderPanel: false,
            showSaleOrderDetailPanel: true
        });
    }
    onClickAddBtnClear = () => {
        if(!this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.choseDealer',
                defaultMessage: '请先选择总代'
            }));
        this.setState({
            showProductCategoryPanel: true,
            details: [],
        });
    }
    onClickAddBtn = () => {
        if(!this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.choseDealer',
                defaultMessage: '请先选择总代'
            }));
        this.setState({
            showProductCategoryPanel: true
        });
    }
    onCloseProductCategoryPanel = () => {
        this.setState({showProductCategoryPanel: false});
    }
    onClickAddProductCategoryBtnClear = () => {
        if(!this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.choseDealer',
                defaultMessage: '请先选择总代'
            }));
        this.setState({
            showProductCategory: true,
            details: [],
        });
    }
    onClickAddProductCategoryBtn = () => {
        if(!this.state.data.dealerId)
            return msg.warn(this.props.intl.formatMessage({
                id: 'NewPanel.message.choseDealer',
                defaultMessage: '请先选择总代'
            }));
        this.setState({
            showProductCategory: true
        });
    }
    onCloseProductCategory = () => {
        this.setState({showProductCategory: false});
    }
    onSelectProductCategory = records => {
        const retailDetails = this.state.details || [];
        const data = this.state.data;
        this.state.data.querySource = true;
        records.forEach(r => {
            const index = retailDetails.findIndex(v => v.productCode === r.lV5Code);
            if(index === -1)
                retailDetails.push({
                    id: r.id,
                    dataSource: 3,
                    sourceCode: null,
                    productGroupId: r.lV4Id,
                    productGroupCode: r.lV4Code,
                    productGroupName: r.lV4Name,
                    productCategoryCode: r.lV2Code,
                    productCategoryName: r.lV2Name,
                    quantity: 1,
                    containerNumber: (data.deliveryTerms === saleOrderTransportType.Others ||
                        data.deliveryTerms === saleOrderTransportType['By Air'] ||
                        data.deliveryTerms === saleOrderTransportType['RO-RO']) ? 0 : 1,
                    totalPrice: 0,
                    versionCode: r.lV3Code,
                    versionName: r.lV3Name,
                    unitVolume: r.unitVolume,
                    productId: r.lV5Id,
                    productCode: r.lV5Code,
                    productName: r.lV5Name,
                    color: r.color,
                    price: 0,
                    unitShipmentAmount: 0,
                    isUnitVolume: Boolean(r.unitVolume)
                });
        });
        this.setState({
            details: [...retailDetails],
            hasOperat: true,
            showProductCategory: false
        });
    }
    onSelectAddedProductCategory = records => {
        const retailDetails = this.state.details || [];
        const data = this.state.data;
        this.state.data.querySource = false;
        records.forEach(r => {
            const index = retailDetails.findIndex(v => v.productGroupCode === r.lV4Code);
            if(index === -1)
                retailDetails.push({
                    id: r.id,
                    dataSource: 3,
                    sourceCode: null,
                    productGroupId: r.lV4Id,
                    productGroupCode: r.lV4Code,
                    productGroupName: r.lV4Name,
                    productCategoryCode: r.lV2Code,
                    productCategoryName: r.lV2Name,
                    quantity: 1,
                    containerNumber: (data.deliveryTerms === saleOrderTransportType.Others ||
                        data.deliveryTerms === saleOrderTransportType['By Air'] ||
                        data.deliveryTerms === saleOrderTransportType['RO-RO']) ? 0 : 1,
                    totalPrice: 0,
                    versionCode: r.lV3Code,
                    versionName: r.lV3Name,
                    unitVolume: r.unitVolume ? r.unitVolume : 0,
                    price: 0,
                    unitShipmentAmount: 0,
                    isUnitVolume: Boolean(r.unitVolume)
                });
        });
        this.setState({
            details: [...retailDetails],
            hasOperat: true,
            showProductCategoryPanel: false
        });
    }
    onDeleteAll = () => {
        this.setState({
            details: [],
            hasOperat: true,
        });
    }
    onDeleteDetail = e => {
        const id = e.target.dataset.id;
        const tmp = this.state.details.filter(d => d.id !== id);
        if(this.state.details.length !== 0)
            this.setState({
                details: [...tmp],
                hasOperat: true,
            });
        if(this.state.details.length === 0)
            this.setState({
                data: {dataSource: undefined}
            });
    }
    onChangePrice = (price, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    price,
                });
            return d;
        });
        
        const data = this.state.data;
        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalInsuranceAmount += (((v.price + (v.unitShipmentAmount * (v.unitVolume ? v.unitVolume : 0))) * 0.044)) * v.quantity;
                });
            if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF || data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
                this.setState({
                    data: Object.assign({}, data, {
                        insuranceAmount: totalInsuranceAmount
                    })
                });
        }
        if((data.salesOrderPriceTerms === saleOrderPriceTerms.CIF || data.salesOrderPriceTerms === saleOrderPriceTerms.CIP) && (data.deliveryTerms === saleOrderTransportType.Container ||
            data.deliveryTerms === saleOrderTransportType.Railway ||
            data.deliveryTerms === saleOrderTransportType['By Sea'])) {
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalInsuranceAmount += ((((v.price ? v.price : 0) + ((v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber)) * 0.044)) * v.quantity;
                });
            this.setState({
                data: Object.assign({}, data, {
                    insuranceAmount: totalInsuranceAmount
                }),
                hasOperat: true
            });
        }
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onUpdateUnitVolume = (unitVolume, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    unitVolume,
                });
            return d;
        });
        const data = this.state.data;
        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
            let totalShipmentAmount = 0;
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                    totalInsuranceAmount += (((v.price + (v.unitShipmentAmount * (v.unitVolume ? v.unitVolume : 0))) * 0.044)) * v.quantity;
                });
            if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF || data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount,
                        insuranceAmount: totalInsuranceAmount
                    })
                });
            else
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount
                    })
                });
        }
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onUpdateQuantity = (quantity, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    quantity,
                });
            return d;
        });
        const data = this.state.data;
        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
            let totalShipmentAmount = 0;
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                    totalInsuranceAmount += (((v.price + (v.unitShipmentAmount * (v.unitVolume ? v.unitVolume : 0))) * 0.044)) * v.quantity;
                });
            if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF || data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount,
                        insuranceAmount: totalInsuranceAmount
                    })
                });
            else
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount
                    })
                });
        }
        if((data.salesOrderPriceTerms === saleOrderPriceTerms.CIF || data.salesOrderPriceTerms === saleOrderPriceTerms.CIP) && (data.deliveryTerms === saleOrderTransportType.Container ||
            data.deliveryTerms === saleOrderTransportType.Railway ||
            data.deliveryTerms === saleOrderTransportType['By Sea'])) {
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalInsuranceAmount += ((((v.price ? v.price : 0) + ((v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber)) * 0.044)) * v.quantity;
                });
            this.setState({
                data: Object.assign({}, data, {
                    insuranceAmount: totalInsuranceAmount
                }),
                hasOperat: true
            });
        }
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onUpdateContainerNumber = (containerNumber, id) => {
        const details = this.state.details || [];
        const data = this.state.data;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    containerNumber,
                });
            return d;
        });
        if(data.deliveryTerms === saleOrderTransportType.Container ||
            data.deliveryTerms === saleOrderTransportType.Railway ||
            data.deliveryTerms === saleOrderTransportType['By Sea']) {
                let totalShipmentAmount = 0;
                let totalInsuranceAmount = 0;
                if(tmp)
                    tmp.forEach(v => {
                        totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber;
                        totalInsuranceAmount += ((((v.price ? v.price : 0) + ((v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber)) * 0.044)) * v.quantity;
                    });
                    this.setState({
                        data: Object.assign({}, data, {
                            shipmentAmount: totalShipmentAmount,
                            insuranceAmount: totalInsuranceAmount
                        })
                    });
        }
        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
            let totalShipmentAmount = 0;
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                    totalInsuranceAmount += (((v.price + (v.unitShipmentAmount * (v.unitVolume ? v.unitVolume : 0))) * 0.044)) * v.quantity;
                });
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount,
                        insuranceAmount: totalInsuranceAmount
                    })
                });
        }
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    unitShipmentAmountOnChange = (unitShipmentAmount, id) => {
        const details = this.state.details || [];
        const data = this.state.data;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    unitShipmentAmount,
                });
            return d;
        });
        if(data.deliveryTerms === saleOrderTransportType.Container ||
            data.deliveryTerms === saleOrderTransportType.Railway ||
            data.deliveryTerms === saleOrderTransportType['By Sea']) {
            let totalShipmentAmount = 0;
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalShipmentAmount += v.unitShipmentAmount * v.containerNumber;
                    totalInsuranceAmount += ((((v.price ? v.price : 0) + ((v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber)) * 0.044)) * v.quantity;
                });
            if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF || data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount,
                        insuranceAmount: totalInsuranceAmount
                    })
                });
            else
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount
                    })
                });
        }
        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
            let totalShipmentAmount = 0;
            let totalInsuranceAmount = 0;
            if(tmp)
                tmp.forEach(v => {
                    totalShipmentAmount += v.unitShipmentAmount * v.unitVolume * v.quantity;
                    totalInsuranceAmount += (((v.price + (v.unitShipmentAmount * (v.unitVolume ? v.unitVolume : 0))) * 0.044)) * v.quantity;
                });
            if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF || data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount,
                        insuranceAmount: totalInsuranceAmount
                    })
                });
            else
                this.setState({
                    data: Object.assign({}, data, {
                        shipmentAmount: totalShipmentAmount
                    })
                });
        }
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    unitInsuranceAmountOnChange = (unitInsuranceAmount, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    unitInsuranceAmount,
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    shipmentAmountSum = (shipmentAmountSum, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    shipmentAmountSum,
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    remarkOnChange = (remark, id) => {
        const details = this.state.details || [];
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    remark,
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onSelectAddedSaleOrderDetail = records => {
        const data = this.state.data;
        const retailDetails = this.state.details || [];
        records.forEach(r => {
            const index = retailDetails.findIndex(v => v.saleOrderDetailId === r.id);
            if(index === -1)
                retailDetails.push({
                    id: r.id,
                    saleOrderDetailId: r.id,
                    dataSource: 1,
                    saleOrderId: r.saleOrderId,
                    sourceCode: r.sourceCode,
                    productGroupId: r.productGroupId,
                    productGroupCode: r.productGroupCode,
                    productGroupName: r.productGroupName,
                    productCategoryId: r.productCategoryId,
                    productCategoryCode: r.productCategoryCode,
                    productCategoryName: r.productCategoryName,
                    quantity: r.quantity,
                    containerNumber: r.containerNumber || (data.deliveryTerms === saleOrderTransportType.Others ||
                        data.deliveryTerms === saleOrderTransportType['By Air'] ||
                        data.deliveryTerms === saleOrderTransportType['RO-RO']) ? 0 : 1,
                    price: r.price,
                    totalPrice: r.totalPrice,
                    unitShipmentAmount: 0,
                    unitInsuranceAmount: 0,
                    unitVolume: r.unitVolume || 0,
                    color: r.color,
                    productCode: r.productCode,
                    oldQuantity: r.quantity,
                    versionCode: r.versionCode,
                    productId: r.productId,
                    isUnitVolume: Boolean(r.unitVolume),
                    currency: r.currency,
                    paymentType: r.paymentType,
                    salesOrderPriceTerms: r.salesOrderPriceTerms,
                    transportType: r.transportType,
                    tradeMethodId: r.tradeMethodId,
                    tradeMethodName: r.tradeMethodName,
                });
        });
        if(data.deliveryTerms === saleOrderTransportType.Container ||
            data.deliveryTerms === saleOrderTransportType.Railway ||
            data.deliveryTerms === saleOrderTransportType['By Sea']) {
            let totalShipmentAmount = 0;
            if(retailDetails)
                retailDetails.forEach(v => {
                    totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber;
                });
            this.setState({
                data: Object.assign({}, data, {
                    shipmentAmount: totalShipmentAmount
                })
            });
        }
        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
            let totalShipmentAmount = 0;
            if(retailDetails)
                retailDetails.forEach(v => {
                    totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                });
            this.setState({
                data: Object.assign({}, data, {
                    shipmentAmount: totalShipmentAmount
                })
            });
        }
        this.setState({
            details: [...retailDetails],
            data: Object.assign({}, this.state.data, {
                currency: records[0].currency,
                paymentType: records[0].paymentType,
                salesOrderPriceTerms: records[0].salesOrderPriceTerms,
                deliveryTerms: records[0].transportType,
                tradeMethodId: records[0].tradeMethodId,
                tradeMethodName: records[0].tradeMethodName,
            }),
            hasOperat: true,
            showSaleOrderDetailPanel: false
        });
    }
    onCloseSaleOrderDetailPanel= () => {
        this.setState({showSaleOrderDetailPanel: false});
    }
    render() {
        const {data, details, isUnitVolume} = this.state;
        const dealerInfo = data.dealerId ? {
            id: data.dealerId,
            code: data.dealerCode,
            name: data.dealerName,
        } : null;
        const validateBrand = !data.brandId && (data.dataSource === 1 ? this.state.valiSaleOrder : this.state.validate) ? 'error' : null;
        const validateDealer = !data.dealerId && (data.dataSource === 1 ? this.state.valiSaleOrder : this.state.validate) ? 'error' : null;
        const validateCurrency = !data.currency && (data.dataSource === 1 ? this.state.valiSaleOrder : this.state.validate) ? 'error' : null;
        const validateTradeMethod = !data.tradeMethodId && (data.dataSource === 1 ? this.state.valiSaleOrder : this.state.validate) ? 'error' : null;
        const validateSalesOrderPriceTerms = !data.salesOrderPriceTerms &&
            (data.dataSource === 1 ? this.state.valiSaleOrder : this.state.validate) ? 'error' : null;
        const validatePaymentType = !data.paymentType && (data.dataSource === 1 ? this.state.valiSaleOrder : this.state.validate) ? 'error' : null;
        const validateDeliveryTerms = !data.deliveryTerms && (data.dataSource === 1
            ? this.state.valiSaleOrder : this.state.validate) ? 'error' : null;
        const validateShippingPort = !data.shippingPortId && this.state.validate ? 'error' : null;
        const validateDestinationPort = !data.destinationPort && this.state.validate ? 'error' : null;
        const validateCommodity = !data.commodity && this.state.validate ? 'error' : null;
        const validateBank = !data.bankId && this.state.validate ? 'error' : null;
        const validateCarrierId = !data.carrierId && this.state.validate ? 'error' : null;
        const validatePayableAccount = !data.payableAccount && this.state.validate ? 'error' : null;
        const {formatMessage} = this.props.intl;
        const {pagePermission} = this.props;
        const submitable = hasPermissions(PERMISSION.add, pagePermission);
        let totalAmount = 0;
        let totalQuantity = 0;
        let totalShipmentAmount = 0;
        let totalInsuranceAmount = 0;
        let totalAmountSum = 0;
        if(details)
            details.forEach(v => {
                totalAmount += (v.quantity ? v.quantity : 0) * (v.price ? v.price : 0);
                totalQuantity += v.quantity;
                if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
                    totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity;
                    totalAmountSum += (((v.price ? v.price : 0) * v.quantity) + ((v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.unitVolume * v.quantity) +
                    ((v.unitInsuranceAmount ? v.unitInsuranceAmount : 0) * v.quantity));
                    totalInsuranceAmount += (((v.price + (v.unitShipmentAmount * (v.unitVolume ? v.unitVolume : 0))) * 0.044)) * v.quantity;
                }
                if(data.deliveryTerms === saleOrderTransportType.Container ||
                    data.deliveryTerms === saleOrderTransportType.Railway ||
                    data.deliveryTerms === saleOrderTransportType['By Sea']) {
                    totalShipmentAmount += (v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber;
                    totalAmountSum += (((v.price ? v.price : 0) * v.quantity) + ((v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber) +
                    ((v.unitInsuranceAmount ? v.unitInsuranceAmount : 0) * v.quantity));
                    totalInsuranceAmount += ((((v.price ? v.price : 0) + ((v.unitShipmentAmount ? v.unitShipmentAmount : 0) * v.containerNumber)) * 0.044)) * v.quantity;
                }
            });
        const columns = [{
            title: formatMessage({
                id: 'newPanel.column.productGroupCode',
                defaultMessage: '物料组编号'
            }),
            dataIndex: 'productGroupCode',
        }, {
            title: formatMessage({
                id: 'newPanel.column.productCode',
                defaultMessage: '产品编号'
            }),
            dataIndex: 'productCode',
        }, {
            title: formatMessage({
                id: 'newPanel.column.versionCode',
                defaultMessage: '版型编号'
            }),
            dataIndex: 'versionCode',
        }, {
            title: formatMessage({
                id: 'newPanel.column.productCategoryCode',
                defaultMessage: '车型编号'
            }),
            dataIndex: 'productCategoryCode',
        }, {
            title: formatMessage({
                id: 'newPanel.column.color',
                defaultMessage: '颜色'
            }),
            dataIndex: 'color',
        }, {
            title: formatMessage({
                id: 'newPanel.column.unitVolume',
                defaultMessage: '单车体积(M3)'
            }),
            dataIndex: 'unitVolume',
            width: 100,
            render: (text, record) => {
                const validate = (!text || text === undefined) || text === 0 ? {validateStatus: 'error'} : null;
                return (<FormItem {...validate}>
                    <LocalizeInputNumber
                        disabled={isUnitVolume}
                        min={0}
                        id={record.id}
                        precision={3}
                        value={text} onChange={this.onUpdateUnitVolume}/>
                </FormItem>);
            }
        }, {
            title: formatMessage({
                id: 'newPanel.column.quantity',
                defaultMessage: '数量'
            }),
            dataIndex: 'quantity',
            width: 120,
            render: (text, record) => {
                const validate = (!text || text === undefined) || text === 0 ? {validateStatus: 'error'} : null;
                return (<FormItem {...validate}>
                    <LocalizeInputNumber
                        min={0}
                        max={data.dataSource === proformaInvoiceSource.整车销售订单 ? (record.oldQuantity || 0) : 99999999}
                        id={record.id}
                        precision={0}
                        value={text} onChange={this.onUpdateQuantity}/>
                </FormItem>);
            }
        }, {
            title: formatMessage({
                id: 'newPanel.column.price',
                defaultMessage: '单价'
            }),
            dataIndex: 'price',
            width: 120,
            render: (text, record) => {
                const validate = (!text || text === undefined) ? {validateStatus: 'error'} : null;
                return (<FormItem {...validate}>
                    <InputAmount
                        precision={2}
                        id={record.id}
                        value={record.price}
                        onChange={this.onChangePrice}/>
                </FormItem>);
            }
        }, {
            title: formatMessage({
                id: 'newPanel.column.totalPrice',
                defaultMessage: '总价'
            }),
            dataIndex: 'totalPrice',
            render: (text, record) => formatAmount((record.quantity ? record.quantity : 0) * (record.price ? record.price : 0))
        }, {
            title: formatMessage({
                id: 'newPanel.column.volumeSum',
                defaultMessage: '总体积(M3)'
            }),
            dataIndex: 'volumeSum',
            render: (text, record) => (record.unitVolume * record.quantity).toFixed(4)
        }, {
            title: formatMessage({
                id: 'newPanel.column.amountSum',
                defaultMessage: '总金额'
            }),
            dataIndex: 'amountSum',
            render: (text, record) => {
                if(data.deliveryTerms === 2)
                    // eslint-disable-next-line no-return-assign
                    return record.amountSum = formatAmount((((record.price ? record.price : 0) * record.quantity) + ((record.unitShipmentAmount ? record.unitShipmentAmount : 0) * record.unitVolume * record.quantity) +
                    ((record.unitInsuranceAmount ? record.unitInsuranceAmount : 0) * record.quantity)));
                if(data.deliveryTerms === 1 || data.deliveryTerms === 3 || data.deliveryTerms === 6)
                    // eslint-disable-next-line no-return-assign
                    return record.amountSum = formatAmount((((record.price ? record.price : 0) * record.quantity) + ((record.unitShipmentAmount ? record.unitShipmentAmount : 0) * record.containerNumber) +
                    ((record.unitInsuranceAmount ? record.unitInsuranceAmount : 0) * record.quantity)));
            }
        }, {
            title: (<span>{formatMessage({
                id: 'newPanel.column.remark',
                defaultMessage: '备注'
            })}</span>
            ),
            dataIndex: 'remark',
            render: (text, record) => (
                <FormItem>
                    <TextInput
                        id={record.id}
                        value={record.remark}
                        onBlur={this.remarkOnChange}/>
                </FormItem>
            )
        }, {
            title: formatMessage({
                id: 'newPanel.column.versionName',
                defaultMessage: '版型名称'
            }),
            dataIndex: 'versionName',
        }, {
            title: formatMessage({
                id: 'newPanel.column.productCategoryName',
                defaultMessage: '车型名称'
            }),
            dataIndex: 'productCategoryName',
        }, {
            title: details.length > 0
                ? (
                    <Popconfirm placement="topRight" title={formatMessage({
                        id: 'newPanel.validation.isDelete',
                        defaultMessage: '是否确认全部删除？'
                    })} onConfirm={this.onDeleteAll} >
                        <a>{formatMessage({
                            id: 'newPanel.validation.clear',
                            defaultMessage: '清空'
                        })}</a>
                    </Popconfirm>
                )
                : null,
            dataIndex: 'id',
            fixed: 'right',
            width: FIXED_COLUMN_WIDTH,
            render: text => <a data-id={text} onClick={this.onDeleteDetail}>{formatMessage({
                id: 'newPanel.validation.delete',
                defaultMessage: '删除'
            })}</a>
        }];
        if(data.dataSource === proformaInvoiceSource.整车销售订单)
            columns.splice(0, 0,
                {
                    title: formatMessage({
                        id: 'newPanel.column.sourceCode',
                        defaultMessage: '源单据号'
                    }),
                    dataIndex: 'sourceCode',
                });
        if(data.dataSource === proformaInvoiceSource.整车销售订单)
            columns.splice(8, 0,
                {
                    title: formatMessage({
                        id: 'newPanel.column.oldQuantity',
                        defaultMessage: '可用数量'
                    }),
                    dataIndex: 'oldQuantity',
                    render: text => text
                });
        if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CPT ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CFR ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
            columns.splice(11, 0,
                {
                    title: formatMessage({
                        id: 'newPanel.column.containerNumber',
                        defaultMessage: '集装箱数'
                    }),
                    dataIndex: 'containerNumber',
                    width: 100,
                    render: (text, record) => {
                        const validate = ((!text || text === undefined) || text === 0) &&
                            (data.deliveryTerms === saleOrderTransportType.Container ||
                                data.deliveryTerms === saleOrderTransportType.Railway ||
                                data.deliveryTerms === saleOrderTransportType['By Sea']) ? {validateStatus: 'error'} : null;
                        return (data.deliveryTerms === saleOrderTransportType.Container ||
                            data.deliveryTerms === saleOrderTransportType.Railway ||
                            data.deliveryTerms === saleOrderTransportType['By Sea']) ? <FormItem {...validate}>
                            <LocalizeInputNumber
                                min={0}
                                id={record.id}
                                precision={0}
                                value={text} onChange={this.onUpdateContainerNumber}/>
                        </FormItem>
                            : text;
                    }
                });
        if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CPT ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CFR ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
            columns.splice(12, 0,
                {
                    title: formatMessage({
                        id: 'newPanel.column.unitShipmentAmount',
                        defaultMessage: '单位运费'
                    }),
                    dataIndex: 'unitShipmentAmount',
                    width: 100,
                    render: (text, record) => {
                        const validate = (!text || text === undefined) &&
                        (data.deliveryTerms === saleOrderTransportType.Container ||
                            data.deliveryTerms === saleOrderTransportType.Railway ||
                            data.deliveryTerms === saleOrderTransportType['By Sea'] ||
                            data.deliveryTerms === saleOrderTransportType['RO-RO']) ? {validateStatus: 'error'} : null;
                        return (data.deliveryTerms === saleOrderTransportType.Container ||
                            data.deliveryTerms === saleOrderTransportType.Railway ||
                            data.deliveryTerms === saleOrderTransportType['By Sea'] ||
                            data.deliveryTerms === saleOrderTransportType['RO-RO']) ? <FormItem {...validate}>
                            <InputAmount
                                precision={2}
                                id={record.id}
                                value={record.unitShipmentAmount}
                                onChange={this.unitShipmentAmountOnChange}/>
                        </FormItem> : text;
                    }
                });
        if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
            columns.splice(13, 0,
                {
                    title: formatMessage({
                        id: 'newPanel.column.unitInsuranceAmount',
                        defaultMessage: '单位保费'
                    }),
                    dataIndex: 'unitInsuranceAmount',
                    render: (text, record) => {
                        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
                            record.unitInsuranceAmount = ((record.price + (record.unitShipmentAmount * (record.unitVolume ? record.unitVolume : 0))) * 0.044);
                            return formatAmount(record.unitInsuranceAmount ? record.unitInsuranceAmount : 0);
                        }
                        if(data.deliveryTerms === saleOrderTransportType.Container ||
                            data.deliveryTerms === saleOrderTransportType.Railway ||
                            data.deliveryTerms === saleOrderTransportType['By Sea']) {
                            record.unitInsuranceAmount = ((record.price + (record.unitShipmentAmount * (record.containerNumber ? record.containerNumber : 0))) * 0.044);
                            return formatAmount(record.unitInsuranceAmount ? record.unitInsuranceAmount : 0);
                        }
                        if(data.deliveryTerms === saleOrderTransportType['By Air'] ||
                            data.deliveryTerms === saleOrderTransportType.Others) {
                                record.unitInsuranceAmount = 0;
                                return 0;
                            }
                    }
                });
        if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CPT ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CFR ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
            columns.splice(14, 0,
                {
                    title: formatMessage({
                        id: 'newPanel.column.shipmentAmountSum',
                        defaultMessage: '总运费'
                    }),
                    dataIndex: 'shipmentAmountSum',
                    render: (text, record) => {
                        if(data.deliveryTerms === saleOrderTransportType['RO-RO']) {
                            record.shipmentAmountSum = record.unitShipmentAmount * record.unitVolume * record.quantity;
                            return formatAmount(record.shipmentAmountSum);
                        }
                        if(data.deliveryTerms === saleOrderTransportType.Container ||
                            data.deliveryTerms === saleOrderTransportType.Railway ||
                            data.deliveryTerms === saleOrderTransportType['By Sea']) {
                            record.shipmentAmountSum = record.unitShipmentAmount * record.containerNumber;
                            return formatAmount(record.shipmentAmountSum);
                        }
                        if(data.deliveryTerms === saleOrderTransportType['By Air'] ||
                        data.deliveryTerms === saleOrderTransportType.Others) {
                            record.shipmentAmountSum = 0;
                            return formatAmount(record.shipmentAmountSum);
                        }
                    }
                });
        if(data.salesOrderPriceTerms === saleOrderPriceTerms.CIF ||
            data.salesOrderPriceTerms === saleOrderPriceTerms.CIP)
            columns.splice(15, 0,
                {
                    title: formatMessage({
                        id: 'newPanel.column.insuranceAmountSum',
                        defaultMessage: '总保费'
                    }),
                    dataIndex: 'insuranceAmountSum',
                    render: (text, record) => {
                        if(data.deliveryTerms === saleOrderTransportType.Container ||
                            data.deliveryTerms === saleOrderTransportType.Railway ||
                            data.deliveryTerms === saleOrderTransportType['By Sea'] ||
                            data.deliveryTerms === saleOrderTransportType['RO-RO']) {
                            record.insuranceAmountSum = record.unitInsuranceAmount * record.quantity;
                            return formatAmount(record.insuranceAmountSum);
                        }
                        if(data.deliveryTerms === saleOrderTransportType['By Air'] ||
                        data.deliveryTerms === saleOrderTransportType.Others) {
                            record.insuranceAmountSum = 0;
                            return formatAmount(record.insuranceAmountSum);
                        }
                    }
                });
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Form>
                        <Card {...FORM_OPTIONS.col} title={formatMessage({
                            id: 'newPanel.card.title',
                            defaultMessage: '基本信息'
                        })}>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={this.props.intl.formatMessage({
                                        id: 'newPanel.form.dataSource',
                                        defaultMessage: '数据来源'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            // eslint-disable-next-line no-unneeded-ternary
                                            disabled={this.props.id ? true : false}
                                            allowClear
                                            name="dataSource"
                                            value={data.dataSource}
                                            options={proformaInvoiceSource.toList(language)}
                                            onChange={this.handleFilterChange}/>
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.brand',
                                            defaultMessage: '品牌'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateBrand} required>
                                        {this.props.initEnum.brands.length !== 1 ? <WrappedSelect
                                            allowClear
                                            name="brandId"
                                            value={data.brandId}
                                            options={this.props.initEnum.brands}
                                            onChange={this.handleFilterChange}/>
                                            : this.props.initEnum.brands[0].text}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.dealerId',
                                            defaultMessage: '总代'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateDealer} required>
                                        {!this.props.id ? <DealerSelectPanel
                                            disabled={this.props.id}
                                            name="dealerId"
                                            value={dealerInfo}
                                            onChange={this.handleFilterChange}
                                            onSelect={this.onSelectDealer} />
                                            : `(${data.dealerCode})${data.dealerName}`}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.batchNumber',
                                            defaultMessage: '批次号'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="batchNumber"
                                            value={data.batchNumber}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.ifUseBatchNumberToPrint',
                                            defaultMessage: '使用批次号打印'
                                        })} {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            allowClear
                                            name="ifUseBatchNumberToPrint"
                                            value={data.ifUseBatchNumberToPrint}
                                            options={isYesNo.toList(language)}
                                            onChange={this.handleFilterChange}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.country',
                                            defaultMessage: '国家'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {data.country}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.countryOfOrigin',
                                            defaultMessage: '原产国'
                                        })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="countryOfOrigin"
                                            value={data.countryOfOrigin}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.countryOfDestination',
                                            defaultMessage: '目的国'
                                        })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="countryOfDestination"
                                            value={data.countryOfDestination}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.address',
                                            defaultMessage: '地址'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        {data.address}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.contactNumber',
                                            defaultMessage: '电话'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="contactNumber"
                                            value={data.contactNumber}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.zipCode',
                                            defaultMessage: '邮政编码'
                                        })} {...FORM_OPTIONS.item}>
                                        {data.zipCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.marketDepartmentName',
                                            defaultMessage: '大区'
                                        })} {...FORM_OPTIONS.item}>
                                        {data.marketDepartmentName}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.currency',
                                            defaultMessage: '币种'
                                        })} {...FORM_OPTIONS.item} validateStatus={validateCurrency} required>
                                        <WrappedSelect
                                            allowClear
                                            name="currency"
                                            value={data.currency}
                                            options={currency.toList(language)}
                                            onChange={this.onClickCurrency}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'queryPanel.form.tradeMethod',
                                            defaultMessage: '贸易方式'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateTradeMethod} required>
                                        <WrappedSelect
                                            allowClear
                                            name="tradeMethodId"
                                            value={data.tradeMethodId}
                                            options={this.props.initEnum.tradeMethods}
                                            onChange={this.onClickTradeMethodId}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.salesOrderPriceTerms',
                                        defaultMessage: '价格条款'
                                    })} {...FORM_OPTIONS.item} validateStatus={validateSalesOrderPriceTerms} required>
                                        <WrappedSelect
                                            allowClear
                                            name="salesOrderPriceTerms"
                                            value={data.salesOrderPriceTerms}
                                            options={saleOrderPriceTerms.toList(language)}
                                            onChange={this.onClickSaleOrderPriceTerms}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.form.paymentType',
                                        defaultMessage: '付款方式'
                                    })} {...FORM_OPTIONS.item} validateStatus={validatePaymentType} required>
                                        <WrappedSelect
                                            allowClear
                                            name="paymentType"
                                            value={data.paymentType}
                                            options={paymentType.toList(language)}
                                            onChange={this.onClickPaymentType}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.text.deliveryTerms',
                                        defaultMessage: '运输方式'
                                    })} {...FORM_OPTIONS.item} validateStatus={validateDeliveryTerms} required>
                                        <WrappedSelect
                                            allowClear
                                            name="deliveryTerms"
                                            value={data.deliveryTerms}
                                            options={saleOrderTransportType.toList(language)}
                                            onChange={this.onClickDeliveryTerms}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.cnpj',
                                            defaultMessage: '法人国家登记号'
                                        })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="cnpj"
                                            value={data.cnpj}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.contact',
                                            defaultMessage: '联系人'
                                        })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="contact"
                                            value={data.contact}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.telex',
                                            defaultMessage: '电传'
                                        })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="telex"
                                            value={data.telex}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                {(data.salesOrderPriceTerms === 3 || data.salesOrderPriceTerms === 5 ||
                                    data.salesOrderPriceTerms === 6 || data.salesOrderPriceTerms === 2) && <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.operation.shipmentAmount',
                                        defaultMessage: '运费汇总'
                                    })} {...FORM_OPTIONS.item}>
                                        <InputAmount
                                            name="shipmentAmount"
                                            precision={2}
                                            value={data.shipmentAmount}
                                            onChange={this.handleFilterChange}/>
                                    </FormItem>
                                </Col>
                                }
                                {(data.salesOrderPriceTerms === 3 || data.salesOrderPriceTerms === 6) && <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.operation.insuranceAmount',
                                        defaultMessage: '保费汇总'
                                    })} {...FORM_OPTIONS.item}>
                                        <InputAmount
                                            name="insuranceAmount"
                                            precision={2}
                                            value={data.insuranceAmount}
                                            onChange={this.handleFilterChange}/>
                                    </FormItem>
                                </Col>
                                }
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.operation.certificationAmount',
                                        defaultMessage: '认证费用'
                                    })} {...FORM_OPTIONS.item}>
                                        <InputAmount
                                            name="certificationAmount"
                                            value={data.certificationAmount}
                                            onChange={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                {data.salesOrderPriceTerms === saleOrderPriceTerms.FCA && <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.payableAccount',
                                            defaultMessage: '到付账号'
                                        })} {...FORM_OPTIONS.item} validateStatus={validatePayableAccount} required>
                                        <TextInput
                                            name="payableAccount"
                                            value={data.payableAccount}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>}
                                {(data.salesOrderPriceTerms === saleOrderPriceTerms.FOB ||
                                    data.salesOrderPriceTerms === saleOrderPriceTerms.FCA ||
                                    data.salesOrderPriceTerms === saleOrderPriceTerms.EXW) &&
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'queryPanel.form.carrier',
                                            defaultMessage: '指定货代'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateCarrierId} required>
                                        <WrappedSelect
                                            name="carrierId"
                                            options={this.state.carrierList}
                                            value={data.carrierId}
                                            onChange={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                }
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'queryPanel.form.shippingPort',
                                            defaultMessage: '发运港口'
                                        })}
                                        {...FORM_OPTIONS.item} validateStatus={validateShippingPort} required>
                                        <WrappedSelect
                                            allowClear
                                            name="shippingPortId"
                                            value={data.shippingPortId}
                                            options={this.props.initEnum.shippingPorts}
                                            onChange={this.handleFilterChange}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.destinationPort',
                                            defaultMessage: '目的港口'
                                        })} {...FORM_OPTIONS.item} validateStatus={validateDestinationPort} required>
                                        <TextInput
                                            name="destinationPort"
                                            value={data.destinationPort}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'queryPanel.form.estimatedDeliveryDate',
                                            defaultMessage: '预计交货日期'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <WrappedDatePicker
                                            name="estimatedDeliveryDate"
                                            value={data.estimatedDeliveryDate}
                                            onChange={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'queryPanel.form.validity',
                                            defaultMessage: '报价有效期'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <WrappedDatePicker
                                            name="validity"
                                            value={data.validity}
                                            onChange={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.formC.commodity',
                                            defaultMessage: 'PI描述'
                                        })} {...FORM_OPTIONS.item} validateStatus={validateCommodity} required>
                                        <TextInput
                                            name="commodity"
                                            value={data.commodity}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.packing',
                                            defaultMessage: '包装描述'
                                        })} {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="packing"
                                            value={data.packing}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={this.props.intl.formatMessage({
                                            id: 'newPanel.form.remark',
                                            defaultMessage: '备注'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <TextInput
                                            name="remark"
                                            value={data.remark}
                                            onBlur={this.handleFilterChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Card>
                        <Card title={formatMessage({
                            id: 'newPanel.column.other',
                            defaultMessage: '银行信息'
                        })}>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.column.bankAccount',
                                        defaultMessage: '银行账号'
                                    })} {...FORM_OPTIONS.item} validateStatus={validateBank} required>
                                        <SearchInput
                                            disabled={true}
                                            value={data.bankAccount}
                                            onClickSearchBtn={this.onClickBankSearchBtn}/>
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'newPanel.operation.bankName',
                                        defaultMessage: '银行名称'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.bankName}
                                    </FormItem>
                                </Col>
                            </Row>
                        </Card>
                        <Card
                            title={<Row>
                                <Col {...FORM_OPTIONS.col}>
                                    {this.props.intl.formatMessage({
                                        id: 'newPanel.details',
                                        defaultMessage: 'PI形式发票清单'
                                    })}
                                </Col>
                                
                            </Row>}
                            extra={<div>
                                <Button
                                    disabled={data.dataSource !== 1}
                                    size="small"
                                    onClick={this.onClickAddSaleOrderBtn}>
                                    {formatMessage({
                                        id: 'newPanel.operation.newSaleOrderDetail',
                                        defaultMessage: '根据订单新增清单'
                                    })}
                                </Button>
                                
                                {this.state.details.length > 0 && this.state.details.some(v => Boolean(v.productId))
                                    ? <Popconfirm placement="topRight" title={formatMessage({
                                        id: 'newPanel.validation.isDelete1',
                                        defaultMessage: '清单数据源为物料号变更为物料组，将会清空清单数据，是否确定？'
                                    })} onConfirm={this.onClickAddBtnClear} >
                                        <Button
                                            disabled={data.dataSource !== 3}
                                            size="small">
                                            {formatMessage({
                                                id: 'newPanel.operation.productCategoryTileds',
                                                defaultMessage: '物料组信息查询'
                                            })}
                                        </Button>
                                    </Popconfirm> : <Button
                                        disabled={data.dataSource !== 3}
                                        size="small"
                                        onClick={this.onClickAddBtn}>
                                        {formatMessage({
                                            id: 'newPanel.operation.productCategoryTileds',
                                            defaultMessage: '物料组信息查询'
                                        })}
                                    </Button>
                                }
                                {this.state.details.length > 0 && this.state.details.some(v => !v.productId)
                                    ? <Popconfirm placement="topRight" title={formatMessage({
                                        id: 'newPanel.validation.isDelete2',
                                        defaultMessage: '清单数据源为物料组变更为物料号，将会清空清单数据，是否确定？'
                                    })} onConfirm={this.onClickAddProductCategoryBtnClear} >
                                        <Button
                                            disabled={data.dataSource !== 3}
                                            size="small">
                                            {formatMessage({
                                                id: 'newPanel.operation.productCategories',
                                                defaultMessage: '产品信息查询'
                                            })}
                                        </Button>
                                    </Popconfirm> : <Button
                                        disabled={data.dataSource !== 3}
                                        size="small"
                                        onClick={this.onClickAddProductCategoryBtn}>
                                        {formatMessage({
                                            id: 'newPanel.operation.productCategories',
                                            defaultMessage: '产品信息查询'
                                        })}
                                    </Button>
                                }
                            </div>}>
                            <Table
                                className="white-space-nowrap"
                                columns={columns}
                                rowKey="id"
                                pagination={false}
                                dataSource={details}
                                footer={() => <DescriptionList>
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalQuantity',
                                        defaultMessage: '数量汇总'
                                    })}>{totalQuantity}
                                    </Description>
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalAmount',
                                        defaultMessage: '总价汇总'
                                    })}><strong>{formatAmount(totalAmount)}</strong>
                                    </Description>
                                    {(data.salesOrderPriceTerms === 3 || data.salesOrderPriceTerms === 5 ||
                                        data.salesOrderPriceTerms === 6 || data.salesOrderPriceTerms === 2) && <Description term={formatMessage({
                                        id: 'newPanel.operation.totalShipmentAmount',
                                        defaultMessage: '运费汇总'
                                    })}>{formatAmount(totalShipmentAmount)}
                                    </Description>}
                                    {(data.salesOrderPriceTerms === 3 || data.salesOrderPriceTerms === 6) && <Description term={formatMessage({
                                        id: 'newPanel.operation.totalInsuranceAmount',
                                        defaultMessage: '保费汇总'
                                    })}>{formatAmount(totalInsuranceAmount)}
                                    </Description>}
                                    <Description term={formatMessage({
                                        id: 'newPanel.operation.totalAmountSum',
                                        defaultMessage: '金额汇总'
                                    })}><strong>{formatAmount(totalAmountSum)}</strong>
                                    </Description>
                                </DescriptionList>}
                                {...TABLE} />
                        </Card>
                        <Card type="inner">
                            <Row>
                                <Col disabled={!submitable} className="col-align-right">
                                    <Button key="submit" loading={this.state.loading}
                                        onClick={this.onSubmit} type="primary">{formatMessage({
                                            id: 'newPanel.btn.submit',
                                            defaultMessage: '保存'
                                        })}</Button>
                                </Col>
                            </Row>
                        </Card>
                    </Form>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        onConfirm={this.onClickReturnBtn}
                        disabled={this.state.loading}
                        showWarning={this.state.hasOperat} />
                </div>
                {
                    this.state.showBankSelectPanel &&
                        <BankSelectPanel
                            selectKey={data.id}
                            brandId={data.brandId}
                            tradeMethodId={data.tradeMethodId}
                            currency={data.currency}
                            onCancel={this.onCloseBankSelectPanel}
                            onSelect={this.onSelectAddedBank} />
                }
                {
                    this.state.showSaleOrderPanel &&
                        <SaleOrderPanel
                            selectKey={data.id}
                            brandId={data.brandId}
                            dealerId={data.dealerId}
                            currency={data.currency}
                            tradeMethodId={data.tradeMethodId}
                            paymentType={data.paymentType}
                            transportType={data.deliveryTerms}
                            salesOrderPriceTerms={data.salesOrderPriceTerms}
                            tradeMethods={this.props.initEnum.tradeMethods}
                            onCancel={this.onCloseSaleOrderPanel}
                            onSelect={this.onSelectSaleOrder} />
                }
                {
                    this.state.showProductCategoryPanel &&
                        <ProductCategoryPanel
                            selectKey={data.id}
                            brandId={data.brandId}
                            dealerId={data.dealerId}
                            currentSelectRowkeys={details}
                            onCancel={this.onCloseProductCategoryPanel}
                            onSelect={this.onSelectAddedProductCategory} />
                }
                {
                    this.state.showProductCategory &&
                        <ProductCategory
                            selectKey={data.id}
                            brandId={data.brandId}
                            dealerId={data.dealerId}
                            currentSelectRowkeys={details}
                            onCancel={this.onCloseProductCategory}
                            onSelect={this.onSelectProductCategory} />
                }
                {
                    this.state.showSaleOrderDetailPanel &&
                        <SaleOrderDetailPanel
                            selectKey={data.id}
                            saleOrderId={data.saleOrderId}
                            currentSelectRowkeys={details}
                            onCancel={this.onCloseSaleOrderDetailPanel}
                            onSelect={this.onSelectAddedSaleOrderDetail} />
                }
            </div>
        );
    }
}

NewPanel.propTypes = {
    initEnum: PropTypes.object.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSubmit: PropTypes.func.isRequired,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
    pagePermission: PropTypes.array,
};
import {connect} from 'react-redux';
import {
    onSubmit,
    tableSearch,
} from './actions';
import {selectorFactory} from 'Shared/utils/immutableToJsSelectorFactory';
const getPagePermission = selectorFactory(['page', 'domainData', 'permissions', 'data']);
import {createSelector} from 'reselect';

const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        const tradeMethods = initData.tradeMethods ? initData.tradeMethods.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        const carriers = initData.carriers ? initData.carriers.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        const shippingPorts = initData.shippingPorts ? initData.shippingPorts.map(item => ({
            text: `${'('}${item.code}${')'}${item.name}`,
            value: item.id
        })) : [];
        return {
            brands,
            tradeMethods,
            carriers,
            shippingPorts
        };
    }
);
const mapStateToProps = state => ({
    pagePermission: getPagePermission(state),
    initEnum: getinitEnumData(state),
});

const mapDispatchToProps = (dispatch, props) => {
    const ownProps = {
        onSubmit: (data, details) => dispatch(onSubmit(data, details)).then(result => {
            if(result.success) {
                dispatch(tableSearch());
                props.history.push(routes.query.url());
            }
        }),
        onCancel: () => props.history.push(routes.query.url()),
    };
    return ownProps;
};

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(NewPanel));
